" An example for a vimrc file.
"
" Maintainer:	Bram Moolenaar <Bram@vim.org>
" Last change:	2006 Nov 16
"
" To use it, copy it to
"     for Unix and OS/2:  ~/.vimrc
"	      for Amiga:  s:.vimrc
"  for MS-DOS and Win32:  $VIM\_vimrc
"	    for OpenVMS:  sys$login:.vimrc

" When started as "evim", evim.vim will already have done these settings.
if v:progname =~? "evim"
  finish
endif

" Use Vim settings, rather then Vi settings (much better!).
" This must be first, because it changes other options as a side effect.
set nocompatible

" allow backspacing over everything in insert mode
set backspace=indent,eol,start

set history=50		" keep 50 lines of command line history
set ruler		" show the cursor position all the time
set showcmd		" display incomplete commands
set incsearch		" do incremental searching

" For Win32 GUI: remove 't' flag from 'guioptions': no tearoff menu entries
" let &guioptions = substitute(&guioptions, "t", "", "g")

" Don't use Ex mode, use Q for formatting
map Q gq

" In many terminal emulators the mouse works just fine, thus enable it.
" set mouse=a

" Switch syntax highlighting on, when the terminal has colors
" Also switch on highlighting the last used search pattern.
if &t_Co > 2 || has("gui_running")
  syntax on
  set hlsearch
endif

" Only do this part when compiled with support for autocommands.
if has("autocmd")

  " Enable file type detection.
  " Use the default filetype settings, so that mail gets 'tw' set to 72,
  " 'cindent' is on in C files, etc.
  " Also load indent files, to automatically do language-dependent indenting.
  filetype plugin indent on

  " Put these in an autocmd group, so that we can delete them easily.
  augroup vimrcEx
  au!

  " For all text files set 'textwidth' to 78 characters.

  autocmd FileType text set textwidth=0

  " When editing a file, always jump to the last known cursor position.
  " Don't do it when the position is invalid or when inside an event handler
  " (happens when dropping a file on gvim).
  autocmd BufReadPost *
    \ if line("'\"") > 0 && line("'\"") <= line("$") |
    \   exe "normal! g`\"" |
    \ endif

  augroup END

else

  set autoindent		" always set autoindenting on

endif " has("autocmd")

" Convenient command to see the difference between the current buffer and the
" file it was loaded from, thus the changes you made.
command DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis
	 	\ | wincmd p | diffthis


" ---------------------------------------------------------------------------------------------------------
" This add by liubin
" ---------------------------------------------------------------------------------------------------------
set tabstop=4
set shiftwidth=4
"set fen
set nofen
set fdm=syntax
set nu
syntax on
"set cindent
set autoindent
filetype plugin indent on
colorscheme lbtorte
"colorscheme desert
set incsearch
set hlsearch
set cino=g0:0     " let class's public: and switch's case  don't indent, 

map <F4> :echo expand("%:p")<CR>
" map <F5> :!dict "<cword>"<CR>
" map <F5> :echo system("dict ".expand("<cword>"))<CR>
map <F5> :echo system("dict ".expand("<cword>"))<CR>
" map <F6> :echo system("sdcv ".expand("<cword>"))<CR>
imap <c-c> <Esc>

"------------------------------------------------------------------------------
""获取当前路径的上一级的路径
function! GET_UP_PATH(dir)
    let pos=len(a:dir)-1
    while pos>0
        if (a:dir[pos]=="/" )
            return  strpart(a:dir,0,pos)
        endif
        let pos=pos-1
    endwhile
    return  ""
endfunction

"设置相关tags
function! s:SET_TAGS()
	let dir = expand("%:p:h") "获得源文件路径
	"在路径上递归向上查找tags文件 
	while dir!=""
		if findfile("tags",dir) !=""
			"找到了就加入到tags
			exec "set tags+=" . dir . "/tags"
		endif
		if findfile("cscope.out",dir) !=""
			if !exists( "g:cscope_added" )
				exec "cs add " . dir . " " . dir
				let g:cscope_added = 1
			endif
		endif
		"得到上级路径
		let dir=GET_UP_PATH(dir)
	endwhile
endfunction

autocmd BufEnter * call s:SET_TAGS()
"------------------------------------------------------------------------------


" add c++ stl tags
set tags+=~/.vim/cplusplustags
let OmniCpp_DefaultNamespaces=["std","_GLIBCXX_STD"]
let OmniCpp_MayCompleteScope=1

set tags+=~/.vim/systags
set tags+=/home/liubin/alipay/src/tb-common/tags


"use winmanager to togggle taglist and netrw
let g:winManagerWindowLayout = 'FileExplorer|TagList'
" nmap wm :WMToggle<cr>

"Taglist
let Tlist_Show_One_File=1
let Tlist_Exit_OnlyWindow=1

" MiniBufExplorer
let g:miniBufExplMapWindowNavVim = 1 
"let g:miniBufExplMapWindowNavArrows = 1
"
"let g:miniBufExplModSelTarget = 1
"let g:miniBufExplorerMoreThanOne = 0
"let g:miniBufExplModSelTarget = 0 
"let g:miniBufExplUseSingleClick = 1
"let g:miniBufExplMapWindowNavVim = 1
"let g:miniBufExplVSplit = 25
"let g:miniBufExplSplitBelow=0
"
" can use TMiniBufExplorer to show or hide minibufexplorer

" define "WM" command to use WMToggle
command WM :let g:miniBufExplorerMoreThanOne = 0 | WMToggle


"A
nnoremap <silent> <F12> :A<CR>


set guifont=Terminus

"for mutt TODO
"autocmd BufReadPost /tmp/mutt-* 
"			\ call append("$","") | call append("$","Best regards") |
"			\ call append("$","---------------------") | call append("$","\tliu_bin@vobile.cn") | call append("$","\t".strftime("%F")) |
autocmd BufReadPost /tmp/mutt-* 
			\ call append( "$", "" ) | call append("$","--") | call append("$","Best regards.") |
autocmd FileType mail set textwidth=0

function! SET_PYTHON_IDENT()
    set shiftwidth=2
    set softtabstop=2
    set tabstop=2
    set backspace=2
    set smarttab
endfunction

au FileType python,ruby,yaml :call SET_PYTHON_IDENT()

function AlignAssignments ()
    "Patterns needed to locate assignment operators...
    let ASSIGN_OP   = '[-+*/%|&]\?=\@<!=[=~]\@!'
    let ASSIGN_LINE = '^\(.\{-}\)\s*\(' . ASSIGN_OP . '\)'

    "Locate block of code to be considered (same indentation, no blanks)
    let indent_pat = '^' . matchstr(getline('.'), '^\s*') . '\S'
    let firstline  = search('^\%('. indent_pat . '\)\@!','bnW') + 1
    let lastline   = search('^\%('. indent_pat . '\)\@!', 'nW') - 1
    if lastline < 0
        let lastline = line('$')
    endif

    "Find the column at which the operators should be aligned...
    let max_align_col = 0
    let max_op_width  = 0
    for linetext in getline(firstline, lastline)
        "Does this line have an assignment in it?
        let left_width = match(linetext, '\s*' . ASSIGN_OP)

        "If so, track the maximal assignment column and operator width...
        if left_width >= 0
            let max_align_col = max([max_align_col, left_width])

            let op_width      = strlen(matchstr(linetext, ASSIGN_OP))
            let max_op_width  = max([max_op_width, op_width+1])
         endif
    endfor

    "Code needed to reformat lines so as to align operators...
    let FORMATTER = '\=printf("%-*s%*s", max_align_col, submatch(1),
    \                                    max_op_width,  submatch(2))'

    " Reformat lines with operators aligned in the appropriate column...
    for linenum in range(firstline, lastline)
        let oldline = getline(linenum)
        let newline = substitute(oldline, ASSIGN_LINE, FORMATTER, "")
        call setline(linenum, newline)
    endfor
endfunction

nmap <silent> ;= :call AlignAssignments()<CR>

source /usr/share/vim/vimcurrent/ftplugin/man.vim

" for haxe
" source /home/liubin/.vim/syntax/haxe.vim
" source /home/liubin/.vim/plugin/haxeOmnicomplete.vim

" temp use
" set nomodeline

set complete=.,w,b,u,t

" dict complete <C-X><C-K>
" from : http://vim.wikia.com/wiki/Dictionary_completions
set dict+=/usr/share/dict/words
" set complete+=k

set wildmode=longest,list,full
set wildmenu

set fencs=utf-8,gbk

" from : http://stackoverflow.com/questions/2549019/how-to-avoid-namespace-content-indentation-in-vim
function! IndentNamespace()
	let l:cline_num = line('.')
	let l:pline_num = prevnonblank(l:cline_num - 1)
	let l:pline = getline(l:pline_num)
	let l:retv = cindent('.')
	while l:pline =~# '\(^\s*{\s*\|^\s*//\|^\s*/\*\|\*/\s*$\)'
		let l:pline_num = prevnonblank(l:pline_num - 1)
		let l:pline = getline(l:pline_num)
	endwhile
	if l:pline =~# '^\s*namespace.*'
		let l:retv = 0
	endif
	return l:retv
endfunction
setlocal indentexpr=IndentNamespace()


au FileType c,cpp :syn keyword cType uchar int8 int16 int32 int64 uint8 uint16 uint32 uint64
au FileType sql :syn keyword sqlKeyword indexwhere hashwhere dochint seghint
au FileType java setlocal omnifunc=javacomplete#Complete

au BufRead,BufNewFile *.g set syntax=antlr3
au BufRead,BufNewFile *.confluence set ft=confluencewiki

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"" Just for note
""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

"" 1. vim 不折行
"" set nowrap 
"" set guioptions+=b 

"" highlight CursorLine cterm=NONE ctermbg=blue ctermfg=NONE guibg=Grey80 guifg=NONE          
"" set cursorline

"" gui options
set guioptions-=m
set guioptions-=T
set guioptions-=r
set guioptions-=R
set guioptions-=l
set guioptions-=L
